Skip to content

test: add unit tests for UserEntity#177

Merged
lindaeskilsson merged 8 commits into
mainfrom
test/UserEntityTest
Apr 13, 2026
Merged

test: add unit tests for UserEntity#177
lindaeskilsson merged 8 commits into
mainfrom
test/UserEntityTest

Conversation

@lindaeskilsson

@lindaeskilsson lindaeskilsson commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Closes #147

Täcker standardvärden, lifecycle callbacks (onCreate, onUpdate), UserDetails-implementationen (getUsername, getPassword, isEnabled, isAccountNonLocked, getAuthorities) samt attachment-hantering (addAttachment, removeAttachment, getUploadedAttachments).

Summary by CodeRabbit

  • Tests
    • Added comprehensive JUnit 5 tests for the User entity covering initial state, lifecycle timestamps (create/update), security/UserDetails behaviors (username, password, enabled/locked), role-based authorities for OWNER/VET/ADMIN, attachment add/remove semantics (uploadedBy handling, null-safety), and unmodifiable uploaded-attachments list.

@coderabbitai

coderabbitai Bot commented Apr 8, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b6e8e152-e30a-43a7-8ec0-a422a66f0008

📥 Commits

Reviewing files that changed from the base of the PR and between 796e27c and 056713c.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/entities/UserEntityTest.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/test/java/org/example/vet1177/entities/UserEntityTest.java

📝 Walkthrough

Walkthrough

Added a JUnit 5 test class validating User entity defaults, lifecycle callbacks (@PrePersist/@PreUpdate), UserDetails methods, role-to-authority mapping, and attachment association behaviors.

Changes

Cohort / File(s) Summary
User Entity Test Suite
src/test/java/org/example/vet1177/entities/UserEntityTest.java
New JUnit 5 tests (≈226 lines) asserting: default state (null id/clinic/timestamps, isActive=true), lifecycle hooks set/refresh timestamps, UserDetails methods (username/password/enabled/locked), role authority strings (ROLE_OWNER/ROLE_VET/ROLE_ADMIN), and attachment add/remove semantics (bidirectional link management, null-safety, unmodifiable returned list).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • annikaholmqvist94
  • TatjanaTrajkovic

Poem

🐇 I hopped to the code with a curious glance,
I checked every timestamp and gave tests a dance,
Roles, attachments, and security too,
All snug in green tests — a rabbit’s review! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 29.63% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'test: add unit tests for UserEntity' accurately and concisely summarizes the main change—adding unit tests for the UserEntity class.
Linked Issues check ✅ Passed The pull request implementation covers all coding requirements from issue #147: @PrePersist/@PreUpdate lifecycle callbacks, default values (isActive=true), role-based authorities (ROLE_OWNER/VET/ADMIN), UserDetails methods (getUsername/getPassword), account status methods (isEnabled/isAccountNonLocked), and attachment management (addAttachment/removeAttachment/getUploadedAttachments).
Out of Scope Changes check ✅ Passed All changes are in-scope test code for UserEntity; no out-of-scope modifications to unrelated files or features are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/UserEntityTest

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/test/java/org/example/vet1177/entities/UserEntityTest.java (1)

19-20: Use a hash-like fixture value for password tests.

Using a plaintext-like literal ("lösenord!") in a passwordHash flow makes the test intent less clear. Prefer a hash-looking fixture string.

Suggested fixture adjustment
-        user = new User("Frida Svensson", "Frida@example.se", "lösenord!", Role.OWNER);
+        user = new User("Frida Svensson", "Frida@example.se", "$2a$10$exampleHashedValue12345678901234567890123456789012", Role.OWNER);
...
-        assertThat(user.getPassword()).isEqualTo("lösenord!");
+        assertThat(user.getPassword()).isEqualTo("$2a$10$exampleHashedValue12345678901234567890123456789012");
Based on learnings: In the `User` entity (`src/main/java/org/example/vet1177/entities/User.java`) the team prefers naming the password field `passwordHash` to clearly signal hashed/encoded storage semantics.

Also applies to: 95-97

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/test/java/org/example/vet1177/entities/UserEntityTest.java` around lines
19 - 20, Replace the plaintext password fixture "lösenord!" in the User test
with a hash-like fixture string to make intent clear (e.g.
"5f4dcc3b5aa765d61d8327deb882cf99") and update any other occurrences (lines
noted around 95-97) to use the same hash-looking value; ensure the test
constructs User using the password/hash parameter expected by the User class
(referencing the User constructor and the passwordHash field) so the test
reflects hashed-password semantics.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/test/java/org/example/vet1177/entities/UserEntityTest.java`:
- Around line 70-76: The test UserEntityTest#onUpdate_shouldRefreshUpdatedAt
only asserts non-null; change it to verify that onUpdate() actually mutates the
timestamp by capturing the previous value (call user.onCreate(); Instant before
= user.getUpdatedAt();) then call user.onUpdate() and assert that
user.getUpdatedAt() is after the captured value (or strictly greater), using the
existing assertion library (e.g., assertThat(...).isAfter(before) or comparable
comparison) so the test fails if updatedAt is not updated.

---

Nitpick comments:
In `@src/test/java/org/example/vet1177/entities/UserEntityTest.java`:
- Around line 19-20: Replace the plaintext password fixture "lösenord!" in the
User test with a hash-like fixture string to make intent clear (e.g.
"5f4dcc3b5aa765d61d8327deb882cf99") and update any other occurrences (lines
noted around 95-97) to use the same hash-looking value; ensure the test
constructs User using the password/hash parameter expected by the User class
(referencing the User constructor and the passwordHash field) so the test
reflects hashed-password semantics.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1dbeedaf-66df-4c90-8364-138c1903b9f1

📥 Commits

Reviewing files that changed from the base of the PR and between 576d18c and 796e27c.

📒 Files selected for processing (1)
  • src/test/java/org/example/vet1177/entities/UserEntityTest.java

Comment thread src/test/java/org/example/vet1177/entities/UserEntityTest.java

@TatjanaTrajkovic TatjanaTrajkovic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ser bra ut!

@lindaeskilsson
lindaeskilsson merged commit 243d567 into main Apr 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test: UserEntityTest – validering, lifecycle och UserDetails

2 participants